//
//  FQFillInfoTableViewCell.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/22.
//

import UIKit

class FQFillInfoTableViewCell: BaseTableViewCell {
    let textField = QMUITextField()
    let titleLb = QMUILabel(font: .rSFont(15), textColor: .color102)
    var checkBlock : (()->Void)?
    let image = QMUIButton()

    var model : FQFillInfoModel! {
        didSet {
            titleLb.text = model.title
            textField.placeholder = model.placeholder
            textField.text = model.detail
            selectionStyle = .none
            image.isHidden = true
            
            if model.type == .city {
                image.isHidden = false
            }
            
            switch model.type {
            case .text:
                textField.keyboardType = .default
                textField.isEnabled = true
            case .phoneNumber, .companyPhone:
                textField.keyboardType = .numberPad
                textField.isEnabled = true
            default:
                selectionStyle = .default
                textField.isEnabled = false
            }
            textField.rx.text.orEmpty.subscribe (onNext: { [unowned self] text in
                self.model.detail = text
                self.checkBlock?()
            }).disposed(by: rx.disposeBag)
        }
    }
    
    lazy var pickView : FQPickInfoView = {
        let pickView = FQPickInfoView()
        pickView.closeBlock = { [unowned self] str in
            textField.text = str
            if fillBlock != nil {
                fillBlock!()
            }
        }
        return pickView
    }()
    
    lazy var rollPickView : FQPickRollView = {
        let rollView = FQPickRollView()
        rollView.closeBlock = { [unowned self] model in
            textField.text = model.value
            self.model.model = model
        }
        return rollView
    }()
    
    var fillBlock : (()->Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.width.equalTo(100)
            make.left.equalTo(20)
        }
        
        textField.font = .rSFont(15)
        textField.textColor = .color102
        contentView.addSubview(textField)
        
        textField.snp.makeConstraints { make in
            make.left.equalTo(titleLb.snp.right).offset(5)
            make.top.bottom.equalToSuperview()
            make.right.equalTo(-10)
        }
        
        image.setImage(UIImage(named: "home_location"), for: .normal)
        image.addTarget(self, action: #selector(location), for: .touchUpInside)
        image.isHidden = true
        contentView.addSubview(image)
        
        image.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.right.equalTo(-10)
            make.width.equalTo(image.snp.width)
        }
    }
    
    func doAction(_ regionData : [FQRegionDataModel]? = nil) {
        if model.type == .address || model.type == .city {
            rollPickView.model = model
            rollPickView.dataArray = regionData!
            rollPickView.show()
        } else if model.type != .address && model.type != .companyPhone && model.type != .phoneNumber && model.type != .text && model.type != .sesame {
            pickView.model = model
            pickView.show()
        } else {
           
        }
    }
    
    @objc func location() {
        NetworkManager.manager.request(targetType: .ipPositioning, responseModelType: NetworkBaseModel<FQLocationModel>.self) { responseModel in
            if responseModel.code != .successCode {
//                    QMUITips.showError(responseModel.msg)
                return
            }
            self.textField.text = responseModel.data!.city
            self.checkBlock?()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
